home *** CD-ROM | disk | FTP | other *** search
-
- double tonearest(double x)
- {
- double i, f, dummy;
-
- /* break x into fractional and integral parts */
- f = fabs(modf(x,&i));
-
- /* if no fractional part, return integer part */
- if (f == 0.0)
- return i;
-
- /* check for a fraction of 1/2 */
- if (f == 0.5)
- {
- /* if i isn't even, set i to nearest even */
- if (modf(i / 2.0, &dummy) != 0.0)
- {
- if (x < 0.0)
- i -= 1.0;
- else
- i += 1.0;
- }
- }
- else
- {
- /* if fraction is greater than 1/2, round i up */
- if (f > 0.5)
- {
- if (x < 0.0)
- i -= 1.0;
- else
- i += 1.0;
- }
- }
-
- return i;
- }
-
-